#include "config.h"
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include "hilog.h"
#include "hifunction.h"

Config *Config::cthis_ = NULL;

Config *Config::instance()
{
    if(cthis_ == NULL)
    {
        cthis_ = new Config();
        cthis_->load();
    }
    return cthis_;
}

void Config::uninstance()
{
    if(cthis_ != NULL)
    {
        delete cthis_;
        cthis_ = NULL;
    }
}

Config::Config()
{
    vec_url_.clear();
}

Config::~Config()
{

}

void Config::load()
{
    string path =  get_app_path()+"neimeng.ini";
    ini_file_.readFile(path.c_str());
    string sec = "NEIMENG_URL";
    vec_url_.push_back(ini_file_.readStr(sec,"url01","rtmp://192.168.1.234/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url02","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url03","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url04","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url05","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url06","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url07","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url08","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url09","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url10","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url11","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url12","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url13","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url14","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url15","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url16","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url17","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url18","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url19","rtmp://192.168.1.240/live/xw"));
    vec_url_.push_back(ini_file_.readStr(sec,"url20","rtmp://192.168.1.240/live/xw"));
    
}
int Config::get_url(const char* ip, unsigned int &unique_code, string &url)
{
    url.clear();    
    if(ip == NULL) {
        hi_warn("ip is null ptr");
        return -1;             
    }   
    if(strlen(ip)< 5) {
        hi_warn("invalid ip: %s", ip);
        return -1;
    }
        
    for(int i = 0 ; i < vec_url_.size() ; i++) { 
        url = vec_url_[i];        
        string::size_type pos = url.find(ip);
        if(pos != url.npos) {                         
            //hi_trace("find url: %s", url.c_str());   
            unique_code = i;
            return 0;  
        }          
    }
    
    hi_warn("ip: %s no find url from ini.", ip);
    return -1;
}







